package com.cakebackoffice.Model;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.cakebackoffice.database.DataSource;
import com.cakebackoffice.database.IDatabasable;
import com.cakebackoffice.database.MenuTable;
import com.cakebackoffice.database.Menu_has_Product;

import java.util.ArrayList;
import java.util.List;

public class Menu implements IDatabasable {
    private Long idDB;
    private Long remoteID;
    private String commentary;
    private MenuType type;
    private List<Product> products = new ArrayList<Product>();

    private Menu(DataSource dataSource, Cursor cursor) {
        idDB = cursor.getLong(cursor.getColumnIndexOrThrow(MenuTable.ID));
        setRemoteID(cursor.getLong(cursor.getColumnIndexOrThrow(MenuTable.REMOTE_ID)));
        commentary = cursor.getString(cursor.getColumnIndexOrThrow(MenuTable.COMMENTS));
        type = MenuType.getOne(dataSource, cursor.getLong(cursor.getColumnIndexOrThrow(MenuTable.ID_MENUTYPE)));

        //Recuperer les ids des Produits
        Cursor cursorp = dataSource.getMore(this, Menu_has_Product.getInstance(), Menu_has_Product.ID_MENU);
        cursorp.moveToFirst();
        while (!cursorp.isAfterLast()) {
            this.getProducts().add(Product.getOne(dataSource, cursorp.getLong(cursorp.getColumnIndexOrThrow(Menu_has_Product.ID_PRODUCT))));
            cursorp.moveToNext();
        }
    }

    public Menu(com.cakebackoffice.clientsoap.contract.Menu menu) {
        //TODO : les menus
    }

    public Long getRemoteID() {
        return remoteID;
    }

    private void setRemoteID(Long remoteID) {
        this.remoteID = remoteID;
    }

    public List<Product> getProducts() {
        return products;
    }

    public void setProducts(List<Product> products) {
        this.products = products;
    }

    public Long getIdDB() {
        return idDB;
    }

    public void setIdDB(Long idDB) {
        this.idDB = idDB;
    }

    public String getCommentary() {
        return commentary;
    }

    public void setCommentary(String commentary) {
        this.commentary = commentary;
    }

    public MenuType getType() {
        return type;
    }

    public void setType(MenuType type) {
        this.type = type;
    }

    @Override
    public ContentValues getValues() {
        ContentValues values = new ContentValues();
        values.put(MenuTable.REMOTE_ID, this.getRemoteID());
        values.put(MenuTable.COMMENTS, this.commentary);
        values.put(MenuTable.ID_MENUTYPE, this.getType() != null ? this.getType().getIdDB() : null);
        return values;
    }


    @Override
    public void saveToDatabase(Context context) {
        //Protection anti-doublons
        Menu bis = null;
        if (this.getIdDB() == null && this.getRemoteID() != null && (bis = Menu.getOneFromRemoteID(context, this.getRemoteID())) != null) {
            this.setIdDB(bis.getIdDB());
        }

        DataSource dataSource = new DataSource(context);
        dataSource.open();
        if (this.getIdDB() == null) {
            dataSource.insert(this, MenuTable.getInstance());
        } else {
            dataSource.update(this, MenuTable.getInstance());
            dataSource.delete(this, Menu_has_Product.getInstance(), Menu_has_Product.ID_PRODUCT);
        }
        {
            for (Product p : this.products) {
                dataSource.insert(Menu_has_Product.getInstance(), p.getIdDB(), this.getIdDB());
            }

        }
        dataSource.close();
    }

    @Override
    public void removeFromDatabase(Context context) {
        if (this.getIdDB() != null) {
            DataSource dataSource = new DataSource(context);
            dataSource.open();
            dataSource.delete(this, MenuTable.getInstance());
            dataSource.delete(this, Menu_has_Product.getInstance(), Menu_has_Product.ID_PRODUCT);
            dataSource.close();
        }


    }

    public static Menu getOneFromRemoteID(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOneFromRemoteID(idDB, MenuTable.getInstance());
        cursor.moveToFirst();
        Menu item = null;
        while (!cursor.isAfterLast()) {
            item = new Menu(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static Menu getOne(Context context, Long idDB) {
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchOne(idDB, MenuTable.getInstance());
        cursor.moveToFirst();
        Menu item = null;
        while (!cursor.isAfterLast()) {
            item = new Menu(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        dataSource.close();
        return item;
    }

    public static List<Menu> getAll(Context context) {
        List<Menu> result = new ArrayList<Menu>();
        DataSource dataSource = new DataSource(context);
        dataSource.open();
        Cursor cursor = dataSource.fetchAll(MenuTable.getInstance());
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            result.add(new Menu(dataSource, cursor));
            cursor.moveToNext();
        }
        dataSource.close();
        return result;
    }

    public static Menu getOne(DataSource dataSource, long idDB) {
        Cursor cursor = dataSource.fetchOne(idDB, MenuTable.getInstance());
        cursor.moveToFirst();
        Menu item = null;
        while (!cursor.isAfterLast()) {
            item = new Menu(dataSource, cursor);
            cursor.moveToNext();
        }
        cursor.close();
        return item;
    }
}
